package com.tebiecloud.mpweixin.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUser;
import com.tebiecloud.mpweixin.server.service.MpweixinUserService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 公众号粉丝信息表 前端控制器
 * @author tebie
 * @date 2019-09-10
 */
@RestController
@RequestMapping("/mpweixinuser")
public class MpweixinUserController {

    @Autowired
    private MpweixinUserService mpweixinUserService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<MpweixinUser>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        MpweixinUser query = pageParams.mapToObject(MpweixinUser.class);
        QueryWrapper<MpweixinUser> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(mpweixinUserService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<MpweixinUser> get(@RequestParam("id") Long id) {
        MpweixinUser entity = mpweixinUserService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param openid 用户的标识，对当前公众号唯一,
     * @param appid 对应公众号appid,
     * @param subscribe 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。,
     * @param nickname 用户的昵称,
     * @param sex 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知,
     * @param city 用户所在城市,
     * @param country 用户所在国家,
     * @param province 用户所在省份,
     * @param language 用户的语言，简体中文为zh_CN,
     * @param headimgurl 用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空。若用户更换头像，原有头像URL将失效。,
     * @param subscribeTime 用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间,
     * @param unionid 只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。,
     * @param remark 公众号运营者对粉丝的备注，公众号运营者可在微信公众平台用户管理界面对粉丝添加备注,
     * @param groupid 用户所在的分组ID（兼容旧的用户分组接口）,
     * @param tagidList 用户被打上的标签ID列表,
     * @param subscribeScene 用户关注的渠道来源，ADD_SCENE_SEARCH 公众号搜索，ADD_SCENE_ACCOUNT_MIGRATION 公众号迁移，ADD_SCENE_PROFILE_CARD 名片分享，ADD_SCENE_QR_CODE 扫描二维码，ADD_SCENEPROFILE LINK 图文页内名称点击，ADD_SCENE_PROFILE_ITEM 图文页右上角菜单，ADD_SCENE_PAID 支付后关注，ADD_SCENE_OTHERS 其他,
     * @param lastVisit 最后访问时间
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "openid", required = false) String openid,
        @RequestParam(value = "appid", required = false) String appid,
        @RequestParam(value = "subscribe", required = false) Integer subscribe,
        @RequestParam(value = "nickname", required = false) String nickname,
        @RequestParam(value = "sex", required = false) Integer sex,
        @RequestParam(value = "city", required = false) String city,
        @RequestParam(value = "country", required = false) String country,
        @RequestParam(value = "province", required = false) String province,
        @RequestParam(value = "language", required = false) String language,
        @RequestParam(value = "headimgurl", required = false) String headimgurl,
        @RequestParam(value = "subscribeTime", required = false) Integer subscribeTime,
        @RequestParam(value = "unionid", required = false) String unionid,
        @RequestParam(value = "remark", required = false) String remark,
        @RequestParam(value = "groupid", required = false) Integer groupid,
        @RequestParam(value = "tagidList", required = false) String tagidList,
        @RequestParam(value = "subscribeScene", required = false) String subscribeScene,
        @RequestParam(value = "lastVisit", required = false) Date lastVisit
    ) {
        MpweixinUser entity = new MpweixinUser();
        entity.setOpenid(openid);
        entity.setAppid(appid);
        entity.setSubscribe(subscribe);
        entity.setNickname(nickname);
        entity.setSex(sex);
        entity.setCity(city);
        entity.setCountry(country);
        entity.setProvince(province);
        entity.setLanguage(language);
        entity.setHeadimgurl(headimgurl);
        entity.setSubscribeTime(subscribeTime);
        entity.setUnionid(unionid);
        entity.setRemark(remark);
        entity.setGroupid(groupid);
        entity.setTagidList(tagidList);
        entity.setSubscribeScene(subscribeScene);
        entity.setLastVisit(lastVisit);
        mpweixinUserService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param openid 用户的标识，对当前公众号唯一
     * @param appid 对应公众号appid
     * @param subscribe 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。
     * @param nickname 用户的昵称
     * @param sex 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
     * @param city 用户所在城市
     * @param country 用户所在国家
     * @param province 用户所在省份
     * @param language 用户的语言，简体中文为zh_CN
     * @param headimgurl 用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空。若用户更换头像，原有头像URL将失效。
     * @param subscribeTime 用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间
     * @param unionid 只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。
     * @param remark 公众号运营者对粉丝的备注，公众号运营者可在微信公众平台用户管理界面对粉丝添加备注
     * @param groupid 用户所在的分组ID（兼容旧的用户分组接口）
     * @param tagidList 用户被打上的标签ID列表
     * @param subscribeScene 用户关注的渠道来源，ADD_SCENE_SEARCH 公众号搜索，ADD_SCENE_ACCOUNT_MIGRATION 公众号迁移，ADD_SCENE_PROFILE_CARD 名片分享，ADD_SCENE_QR_CODE 扫描二维码，ADD_SCENEPROFILE LINK 图文页内名称点击，ADD_SCENE_PROFILE_ITEM 图文页右上角菜单，ADD_SCENE_PAID 支付后关注，ADD_SCENE_OTHERS 其他
     * @param lastVisit 最后访问时间
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "openid", required = false) String openid,
            @RequestParam(value = "appid", required = false) String appid,
            @RequestParam(value = "subscribe", required = false) Integer subscribe,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "sex", required = false) Integer sex,
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "country", required = false) String country,
            @RequestParam(value = "province", required = false) String province,
            @RequestParam(value = "language", required = false) String language,
            @RequestParam(value = "headimgurl", required = false) String headimgurl,
            @RequestParam(value = "subscribeTime", required = false) Integer subscribeTime,
            @RequestParam(value = "unionid", required = false) String unionid,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "groupid", required = false) Integer groupid,
            @RequestParam(value = "tagidList", required = false) String tagidList,
            @RequestParam(value = "subscribeScene", required = false) String subscribeScene,
            @RequestParam(value = "lastVisit", required = false) Date lastVisit
    ) {
        MpweixinUser entity = new MpweixinUser();
        entity.setOpenid(openid);
        entity.setAppid(appid);
        entity.setSubscribe(subscribe);
        entity.setNickname(nickname);
        entity.setSex(sex);
        entity.setCity(city);
        entity.setCountry(country);
        entity.setProvince(province);
        entity.setLanguage(language);
        entity.setHeadimgurl(headimgurl);
        entity.setSubscribeTime(subscribeTime);
        entity.setUnionid(unionid);
        entity.setRemark(remark);
        entity.setGroupid(groupid);
        entity.setTagidList(tagidList);
        entity.setSubscribeScene(subscribeScene);
        entity.setLastVisit(lastVisit);
        mpweixinUserService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            mpweixinUserService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            mpweixinUserService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
